package com.huangmb.anymark.app.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.huangmb.androidlib.utils.ToastUtils;
import com.huangmb.anymark.app.R;
import com.huangmb.anymark.app.activity.UserCenterActivity;
import com.huangmb.anymark.app.api.BaseApi;
import com.huangmb.anymark.app.api.UserMarkerApi;
import com.huangmb.anymark.app.fragment.marker.FoodShopFragment;
import com.huangmb.anymark.app.model.UserMarker;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hmb
 * @version 1.0
 * @since 2016/5/9 21:42
 */
public class UserCenterMarkerAdapter extends RecyclerView.Adapter<UserCenterMarkerAdapter.ViewHolder>{

    private List<UserMarker> mMarkerList = new ArrayList<>();

    private UserCenterActivity mContext;
    public UserCenterMarkerAdapter(UserCenterActivity context,List<UserMarker> list){
        this.mContext = context;
        this.mMarkerList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.self_poi_list_item,parent,false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setUpView(position);
    }

    @Override
    public int getItemCount() {
        return mMarkerList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TextView mPositionText;
        private TextView mTitle;
        private TextView mType;
        private ImageView mModifyBtn;
        private ImageView mDeleteBtn;
        private int mPosition;

        public ViewHolder(View itemView) {
            super(itemView);
            mPositionText = (TextView) itemView.findViewById(R.id.position);
            mTitle = (TextView) itemView.findViewById(R.id.title);
            mType = (TextView) itemView.findViewById(R.id.type);
            mModifyBtn = (ImageView) itemView.findViewById(R.id.modify);
            mDeleteBtn = (ImageView) itemView.findViewById(R.id.delete);
        }
        public void setUpView(final int position){
            UserMarker marker = mMarkerList.get(position);
            mPosition = position;
            mPositionText.setText((position+1)+".");
            mTitle.setText(marker.getTitle());
            mType.setText(marker.getCategory().getName());
            mModifyBtn.setOnClickListener(this);
            mTitle.setOnClickListener(this);
            mDeleteBtn.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.title:
                    //ToastUtils.showShort(mContext,"查看第"+mPosition+"条");
                    mContext.gotoDetailPage(mMarkerList.get(mPosition));
                    break;
                case R.id.modify:
                    //ToastUtils.showShort(mContext,"修改第"+mPosition+"条");
                    mContext.gotoModifyPage(mMarkerList.get(mPosition));
                    break;
                case R.id.delete:
                    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                    builder.setTitle("警告");
                    builder.setMessage("确认删除?");
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //dialog.dismiss();
                        }
                    });
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            UserMarkerApi.delete(mMarkerList.get(mPosition).getId(),new BaseApi.AbsListener(){
                                @Override
                                public void onSuccess(String result) {
                                    super.onSuccess(result);
                                    mMarkerList.remove(mPosition);
                                    notifyDataSetChanged();
                                }
                            });
                        }
                    });
                    builder.show();
                    break;

            }
        }
    }
}
